package com.zggk.lzz.guide;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.ToastUtils;
import com.zggk.lzz.R;
import com.zggk.lzz.api.MyApplication;
import com.zggk.lzz.land.LandActivity;
import com.zggk.lzz.land.UserInfo;
import com.zggk.lzz.mvp.MVPBaseActivity;
import com.zggk.lzz.roadh.RoadHActivity;
import com.zggk.lzz.roadz.RoadZActivity;
import com.zggk.lzz.util.Utils;

import org.json.JSONException;
import org.json.JSONObject;


/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class GuideActivity extends MVPBaseActivity<GuideContract.View, GuidePresenter> implements GuideContract.View {
    private Handler handler;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initdate();
    }

    @Override
    protected int getLayout() {
        return R.layout.act_guide;
    }

    private void initdate() {
        handler = new Handler();
        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            finish();
        } else {
            if (Utils.isNull(MyApplication.spUtils.getString("Token", ""))) {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        startActivity(new Intent(GuideActivity.this, LandActivity.class));
                        finish();
                    }
                }, 500);
            } else {
                mPresenter.getUserInfo(GuideActivity.this);
            }
        }
    }

    @Override
    public void onRequestError(String msg) {

    }

    @Override
    public void getUserInfos(String json) {
        try {
            JSONObject jsonObject = new JSONObject(json);
            String str1 = jsonObject.optString("code");
            if (str1.equals("200")) {
                UserInfo landBean = JSON.parseObject(json, UserInfo.class);
                if (landBean.getData().getRecords().getRoleId().contains("726bb579-86f7-11eb-98a5-0242ac110005")) {//县路长
                    startActivity(new Intent(GuideActivity.this, RoadZActivity.class));
                    finish();
                } else if (landBean.getData().getRecords().getRoleId().contains("72702f76-86f7-11eb-98a5-0242ac110005")) {//护路员
                    startActivity(new Intent(GuideActivity.this, RoadHActivity.class));
                    finish();
                }else {
                    ToastUtils.showShort("当前用户没有路长制权限,请重新登陆");
                    MyApplication.spUtils.clear();
                    startActivity(new Intent(GuideActivity.this, LandActivity.class));
                    finish();
                }
            }else {
                MyApplication.spUtils.clear();
                startActivity(new Intent(GuideActivity.this, LandActivity.class));
                finish();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
